# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

default_platform(:ios)

# 网络请求依赖
require 'net/http'
require 'uri'
require 'json'


platform :ios do
  ipa_dir  = "fastlane_build/"
  lane :beta do |options|
    desc "打包蒲公英测试版"
    # add actions here: https://docs.fastlane.tools/actions
    
    # 编译、打包
    ipa_name = "SitPosition" + Time.new.strftime('%Y-%m-%d_%H:%M')
    build_app(
      # 打包前clean项目
      clean: false,
      # 构建时，隐藏不必要的信息  
      silent: true,
      # 导出方式 app-store、ad-hoc、enterprise、development
      export_options: {
        method: "ad-hoc",
        # iCloudContainerEnvironment: "Production",
        # teamID: "2W4KJH5NL5",
        # provisioningProfiles: { "com.imoreapps.SitPosition": "jrw2100-adhoc" }
      },
      export_method: "ad-hoc",
      # scheme
      scheme: "SitPosition",
      # workspace
      workspace: "SitPosition.xcworkspace",
      # bitcode
      include_bitcode: false,
      # 环境 Debug、Release
      configuration: "Release",
      # ipa的存放目录
      output_directory: ipa_dir,          
      # 输出ipa的文件名为当前的build号
      output_name: ipa_name
    )

    # 蒲公英
    pgyer(
      api_key: "3794f4411f66c500ab704ba3bc440704", 
      user_key: "2e8dccd2be4fce02ff190a380d36ad21",
      update_description: options[:desc]
    )
  end


  # ----------------------------------------------------------------------
  # ----------------------------------------------------------------------


  lane :release do |options|
    desc "打包AppStore版本"
    # add actions here: https://docs.fastlane.tools/actions
    # Increment the build number (not the version number)
    # Providing the xcodeproj is optional
    increment_build_number(xcodeproj: "SitPosition.xcodeproj")

    # 编译、打包
    ipa_name = "SitPosition" + Time.new.strftime('%Y-%m-%d_%H:%M')
    build_app(
      # 打包前clean项目
      clean: true,
      # 构建时，隐藏不必要的信息  
      silent: true,
      # 导出方式 app-store、ad-hoc、enterprise、development
      export_method: "app-store",
      # scheme
      scheme: "SitPosition",
      # workspace
      workspace: "SitPosition.xcworkspace",
      # symbol
      include_symbols: true,
      # bitcode
      include_bitcode: false,
      # 环境 Debug、Release
      configuration: "Release",
      # ipa的存放目录
      output_directory: ipa_dir,          
      # 输出ipa的文件名为当前的build号
      output_name: ipa_name
    )

    # 上传deliver
    app_path = ipa_dir + "/#{ipa_name}.ipa"
    deliver(
      submit_for_review: true,
      force: true
    )
  end
end
